package com.example.dialog.Adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.dialog.R;
import com.example.dialog.model.Article;

import java.util.ArrayList;
import java.util.List;

public class ArticleDetailsAdapter extends RecyclerView.Adapter<ArticleDetailsAdapter.ViewHolder> {

    private List<Article> articles = new ArrayList<>();

    // 设置数据源
    public void setArticles(List<Article> articles) {
        this.articles = articles;
        notifyDataSetChanged();  // 数据源更新时，通知适配器刷新
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 使用 LayoutInflater 来加载视图
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_article, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Article article = articles.get(position);
        // 设置视图中的数据
        holder.title.setText(article.getTitle());
        holder.content.setText(article.getContent());
        holder.author.setText(article.getAuthor());
        holder.readCount.setText(String.valueOf(article.getReadCount()));
    }

    @Override
    public int getItemCount() {
        return articles.size();
    }

    // ViewHolder类
    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        TextView content;
        TextView author;
        TextView readCount;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            // 初始化视图控件
            title = itemView.findViewById(R.id.tv_title);
            content = itemView.findViewById(R.id.tv_content);
            author = itemView.findViewById(R.id.tv_author);
            readCount = itemView.findViewById(R.id.tv_read_count);
        }
    }
} 